<div id="dates">
    <div style="padding-right:0px;">
        <table class="dates-table">
            <tbody >
                <tr id="months-row">
                    <th id="col1" class="th-checkin-date" title="">
            <div style="margin-left:0px !important;" id="search-date-from"> Đổi ngày nhận phòng<input class="hasDatepicker" id="search-from" value="<?php echo $date; ?>" onfocus="showCalendarControl(this);" type="text"/>
            </div>
            </th>
            <th id="col2" class="fullrate">
            <div id="display_rates">
                <a class="changedate" href="javascript:getDealNo();">Đổi ngày </a>
            </div>
            </th>
            <?php
            if ($ngaybatdau != 0) {
                $startdate = gregoriantojd($thang, $ngay, $nam);
                $ngay2 = $ngay + 14;
                $thang2 = $thang;
                $nam2 = $nam;
                if ($ngay2 > $songay) {
                    $ngay2 = $ngay2 - $songay;
                    $thang2 = $thang + 1;
                    if ($thang2 > 12) {
                        $thang2 = 1;
                        $nam2 = $nam + 1;
                    }
                }

                $enddate = gregoriantojd($thang2, $ngay2, $nam2);
                $datebd = gregoriantojd($thangbatdau, $ngaybatdau, $nambatdau);
                $datekt = gregoriantojd($thangketthuc, $ngayketthuc, $namketthuc);
                if ($datebd < $startdate) {
                    if ($datekt < $startdate) {
                        $vitribd = -1;
                        $vitrikt = -1;
                    } else {
                        if ($datekt >= $enddate) {
                            $vitribd = 0;
                            $vitrikt = 13;
                        } else {
                            $vitribd = 0;
                            $vitrikt = $datekt - $startdate;
                        }
                    }
                } else {
                    if ($datebd >= $enddate) {
                        $vitribd = -1;
                        $vitrikt = -1;
                    } else {
                        if ($datekt >= $enddate) {
                            $vitribd = $datebd - $startdate;
                            $vitrikt = 13;
                        } else {
                            $vitribd = $datebd - $startdate;
                            $vitrikt = $datekt - $startdate;
                        }
                    }
                }
                $class = 'deals select-col1 ';
                for ($i = $vitribd; $i <= $vitrikt; $i++) {
                    $class = $class . ' select' . $i;
                }
            }
            $d = $songay - $ngay + 1;
            $ts = mktime(0, 0, 0, $thang, $ngay, $nam);
            $date1 = date("Y-m-d", $ts);
            $now = date("Y-m-d");
            if ($d >= 14) {
                ?>
                <th class="month endofmonth" colspan="14">
                <div class="month-inner">
                    <?php if ($now != $date1) { ?>
                        <a href="javascript:void(0)" class="button" id="prevbutton" title=" 7 ngày trước" onclick="preCalendar(1353081600);"> 7 ngày trước</a>
                    <?php } ?>
                    <span class="label" style="margin-top: 2px;"><?php echo 'Tháng ' . $thang . ', ' . $nam; ?></span>
                    <a href="javascript:void(0)" class="button  zzz" id="nextbutton" title=" 7 ngày tới"> 7 ngày tới</a>
                </div>
                <input id="startMonthId" value="" type="hidden"/>
                </th>
            <?php } else { ?>
                <th class="month endofmonth" colspan="<?php echo $d; ?>">
                <div class="month-inner">
                    <?php if ($now != $date1) { ?>
                        <a href="javascript:void(0)" class="button" id="prevbutton" title=" 7 ngày trước" onclick="preCalendar(1353081600);"> 7 ngày trước</a>
                    <?php } ?>
                    <span class="label" style="margin-top: 2px;"><?php if ($d > 2) echo 'Tháng ' . $thang . ', ' . $nam; ?></span>
                </div>
                <input id="startMonthId" value="" type="hidden"/>
                </th>
                <th class="month startofmonth" colspan="<?php
                $d = 14 - $d;
                echo $d;
                    ?>">
                <div class="month-inner">
                    <span class="label" style="margin-top: 2px;"><?php
                $t = $thang + 1;
                if ($t > 12)
                    $t = 1; if ($d > 2)
                    echo 'Tháng ' . $t . ', ' . $nam;
                    ?></span>
                    <a href="javascript:void(0)" class="button  zzz" id="nextbutton" title=" 7 ngày tới"> 7 ngày tới</a>
                </div>
                <input id="startMonthId" value="" type="hidden"/>
                </th>
            <?php } ?>
            </tr>
            <tr class="head-row">
                <th id="col1" class="property" title="">
            <div class="property-inner">
                <span class="label"> Loại phòng</span>
            </div>
            </th>
            <th id="col2" class="fullrate">
            <div class="property-inner">
                <span class="label"> Giá công bố</span>
            </div>
            </th>
            <?php
            $endmonth = -1;
            $dayofweek = $thu;
            for ($i = 0; $i < 14; $i++) {
                ?>
                <td id="col3">
                    <div class="date-inner">
                        <span class="day"><?php if ($dayofweek == 1) echo 'CN'; else echo 'T' . $dayofweek; ?></span><br>
                        <span class="number"><?php
            $d = $ngay + $i;
            if ($d < $songay + 1)
                echo $d; else {
                $d = $d - $songay;
                echo $d;
                if ($d == 1)
                    $endmonth = $i - 1;
            }
                ?>  </span>
                    </div>
                </td>
                <?php
                $dayofweek = $dayofweek + 1;
                if ($dayofweek > 7)
                    $dayofweek = 1;
            }
            ?>
            <input id="datefrom" value="2012-11-17" type="hidden"/>
            </tr>
            <tr id="dates-row">
                <th style="text-align:left;" colspan="2">Vui lòng chọn số đêm ở tại đây&nbsp;
                    <img src="resources/home/images/shared/arrow1_061.gif"/>
                </th>
            <form name="myform" action="checkboxes.asp" method="post">
                <?php for ($i = 0; $i < 14; $i++) {
                    ?>
                    <td class="date weekend <?php if ($i == $endmonth) echo 'endofmonth' ?>">
                        <input id="deal<?php echo $i; ?>" type="checkbox" <?php
                if ($ngaybatdau != 0) {
                    if ($i >= $vitribd)
                        if ($i <= $vitrikt)
                            echo 'checked';
                        else
                            echo '';
                    else
                        echo '';
                        ?> <?php
                    if ($i > $vitribd)
                        if ($i < $vitrikt)
                            echo 'disabled';
                        else
                            echo '';
                    else
                        echo '';
                }
                    ?> name="chk" onclick="Check(<?php echo $i; ?>)"/>
                    </td>
                <?php }
                ?>
                </tr>
            </form>
            </tbody>
            <?php
            $n = $ngay + 7;
            if ($n < $songay) {
                $ngay_ = $n;
                $thang_ = $thang;
            } else {
                $ngay_ = $n - $songay;
                $thang_ = $thang + 1;
            }
            $nam_ = $nam;
            if ($thang_ == 13) {
                $thang_ = 1;
                $nam_ = $nam_ + 1;
            }
            $ts = mktime(0, 0, 0, $thang_, 1, $nam_);
            $songay_ = date('t', $ts);
            ?>
            <input type="hidden" value="<?php echo $ngay; ?>" name="ngay" id="ngay"/>
            <input type="hidden" value="<?php echo $thang; ?>" name="thang" id="thang"/>
            <input type="hidden" value="<?php echo $nam; ?>" name="nam" id="nam"/>
            <input type="hidden" value="<?php echo $songay; ?>" name="songay" id="songay"/>
            <input type="hidden" value="<?php echo $ngay_; ?>" name="ngay1" id="ngay1"/>
            <input type="hidden" value="<?php echo $thang_; ?>" name="thang1" id="thang1"/>
            <input type="hidden" value="<?php echo $nam_; ?>" name="nam1" id="nam1"/>
            <input type="hidden" value="<?php echo $songay_; ?>" name="songay1" id="songay1"/>
            <input type="hidden" value="<?php echo $thu; ?>" name="thu" id="thu"/>
            <?php
            $n = $ngay - 7;
            $nam_2 = $nam;
            $thang_2 = $thang;
            if ($n >= 0) {
                $ngay_2 = $n;
            } else {
                $thang_2 = $thang - 1;
                if ($thang_2 == 0) {
                    $thang_2 = 12;
                    $nam_2 = $nam - 1;
                }
                $ts = mktime(0, 0, 0, $thang_2, 1, $nam_2);
                $songay_2 = date('t', $ts);
                $ngay_2 = $n + $songay_2;
            }
            $ts = mktime(0, 0, 0, $thang_2, 1, $nam_2);
            $songay_2 = date('t', $ts);
            ?>
            <input type="hidden" value="<?php echo $ngay_2; ?>" name="ngay2" id="ngay2"/>
            <input type="hidden" value="<?php echo $thang_2; ?>" name="thang2" id="thang2"/>
            <input type="hidden" value="<?php echo $nam_2; ?>" name="nam2" id="nam2"/>
            <input type="hidden" value="<?php echo $songay_2; ?>" name="songay2" id="songay2"/>
            <input type="hidden" value="<?php echo $thu; ?>" name="thu2" id="thu2"/>
        </table>
    </div>
</div>
<div class="" style="padding-right:0px;">
    <?php
    $j = 0;
    foreach ($records as $row):
        ?>
        <div class="room">
            <table id="tab_book_<?php echo $j; ?>" class="<?php if ($ngaybatdau != 0) echo $class; else echo 'deals select-col1 ' ?>">
                <tbody>
                    <tr id="property-1" class="rate valid" style="padding-left:0px !important;">
                        <!--AdvancePurchase PoolVilla-->
                        <th class="col1 property" style="padding-left:0px !important;">
                            <a href="javascript:;" class="name hotelname_1"><?php echo $row->Ten; ?></a>
                            <span class="guests"><span id="min_nigth_stay"> tối thiểu = <?php echo $row->SoDemToiThieu ?> đêm<br/> Giá cho <?php echo $row->SoNguoiLonMacDinh ?> người (Tối đa <?php echo $row->SoNguoiToiDa ?>).</span>
                <div>
                    <a style="margin-left:167px;margin-top: -10px;position:absolute;" class="book button vi" id="<?php echo $row->MaLP; ?>" href="javascript: submitform('<?php echo $row->MaLP; ?>')" >BOOK_IVIVU</a>
                </div>
                </th>
                <th class="col2 fullrateroom">
                    <span class="label"><div class="price_fullrate"><?php echo number_format($row->GiaTruocKhuyenMai * 1000, 0, ',', '.'); ?></div>VND</span>
                </th>
                <?php
                $dayofweek = date('w') + 1;
                for ($i = 0; $i < 14; $i++) {
                    ?>
                    <td class="item col<?php echo $i + 3; ?> <?php if ($dayofweek == 1 || $dayofweek == 7) echo 'weekend'; ?> <?php if ($i == $endmonth) echo 'endofmonth'; ?> <?php if ($records1[$row->MaLP][$i]->max == 0) echo 'sold' ?>" onmouseover="ivivu.tooltip.show('6.060.000 VND','1','0','0');">
                                                            <div class="<?php if ($records1[$row->MaLP][$i]->max > 0) echo 'item';?> deal">
                                                                <span class="label"><?php if ($records1[$row->MaLP][$i]->max > 0) echo number_format($records1[$row->MaLP][$i]->gia,  0, ',', '.'); else echo 'SOLD' ?></span>
                                                                <div id="<?php echo $j . '_' . $i; ?>" class="total" style="right: 92%"></div>
																<div class="tooltip_description" style="display:none" title="">
																	<div style="width:38%; align:center; float:left" align="center">
																		<h1 style='background-color: rgb(155, 239, 74); color:black;font-size: 25px;line-spacing:2px;'><?php echo number_format($records1[$row->MaLP][$i]->gia * 1000,  0, ',', '.')?></h1>
																	</div>
																	<div style="width:57%;float:right; align:left;margin-top: 10px">
																		<h4 style="color:rgb(155, 239, 74)"><?php echo $row->Ten; ?></h4>
																	</div>
																	<div style="margin-top: 50px">Include breakfast, airport transfer, welcome fruits basket</div>
																</div>
                                                            </div>
                                                            <div style="display:none" id="description_1_0">Include breakfast, airport transfer, welcome fruits basket</div>
															
                                                        </td>
                    <?php
                    $dayofweek = $dayofweek + 1;
                    if ($dayofweek > 7)
                        $dayofweek = 1;
                }
                ?>
                </tr>
                </tbody>
            </table>
            <div style="clear: both;"></div></div>
        <?php
        $j++;
    endforeach;
    ?>
    <div style="clear: both;"></div>
    <input id="soloaiphong" name="soloaiphong" value="<?php echo $j;?>" type="hidden"/>
</div>
<div style="clear: both;"></div>
<script type="text/javascript">
$j = jQuery.noConflict();
      $j(document).ready(function(){
        $j("div.item").tooltip();
      });
    $('#nextbutton').click(function(){
        var form_data = {
            ngay:$('#ngay1').val(), thang:$('#thang1').val(), nam: $('#nam1').val(),
            songay:$('#songay1').val(), thu:$('#thu').val(),
            khachsan: $('#makhachsan').val(),
            ngaybatdau:$('#ngaybatdau').val(), ngayketthuc:$('#ngayketthuc').val(),
            thangbatdau:$('#thangbatdau').val(), thangketthuc:$('#thangketthuc').val(),
            nambatdau:$('#nambatdau').val(), namketthuc:$('#namketthuc').val(),
            date:$('#search-from').val()
        };
        $.ajax({
            url: "<?php echo site_url('datphong/loadbanggia'); ?>",
            type:"POST",
            data: form_data,
            dataType: 'html',
            success: function(msg){
                $('#banggia').empty();
                $('#banggia').append(msg);
            }
        });
    }
);
    $('#prevbutton').click(function(){
        var form_data = {
            ngay:$('#ngay2').val(),thang:$('#thang2').val(), nam: $('#nam2').val(),
            songay:$('#songay2').val(),thu:$('#thu').val(),
            khachsan: $('#makhachsan').val(),
            ngaybatdau:$('#ngaybatdau').val(), ngayketthuc:$('#ngayketthuc').val(),
            thangbatdau:$('#thangbatdau').val(), thangketthuc:$('#thangketthuc').val(),
            nambatdau:$('#nambatdau').val(), namketthuc:$('#namketthuc').val(),
            date:$('#search-from').val()
        };
        $.ajax({
            url: "<?php echo site_url('datphong/loadbanggia'); ?>",
            type:"POST",
            data: form_data,
            dataType: 'html',
            success: function(msg){
                $('#banggia').empty();
                $('#banggia').append(msg);
            }
        });
    }
);
    $('a').click(function() {
        var value = $(this).attr("id");
        if($(this).hasClass('book'))
        {
            if($('#ngaybatdau').val()==0)
            {
                alert('Quý khách vui lòng chọn số đêm ở.');
                return;
            }
            if(parseInt($('#min_nigth_stay_' + value).val())>parseInt($('#sodemo').val()))
            {
                var min_nigth_stay = $('#min_nigth_stay_' + value).val();
                alert('Báº¡n cáº§n Ä‘Äƒng kÃ½ Ã­t nháº¥t lÃ  ' + min_nigth_stay + ' ngÃ y.');
                return;
            }
            $('#loaiphong').val(value);
            var form_data = {
                loaiphong: $('#loaiphong').val(),
                ngaybatdau:$('#ngaybatdau').val(), ngayketthuc:$('#ngayketthuc').val(),
                thangbatdau:$('#thangbatdau').val(), thangketthuc:$('#thangketthuc').val(),
                nambatdau:$('#nambatdau').val(), namketthuc:$('#namketthuc').val()
            };
            $.ajax({
                url: "<?php echo site_url('datphong/kiemtradatphong'); ?>",
                type:"POST",
                data: form_data,
                dataType: 'html',
                success: function(msg){
                    if(msg == 1)
                        $('#myform').submit();
                    else alert('Một trong những ngày bạn chọn đã hết phòng. Vui lòng chọn lại ngày khác.');
                }
            });
        }
        if($(this).hasClass('changedate'))
        {                                                                            
            var form_data = {
                ngay:$('#search-from').val(),thu:$('#thu').val(),khachsan: $('#makhachsan').val()
            };
            $.ajax({
                url: "<?php echo site_url('datphong/loadbanggiatheongay'); ?>",
                type:"POST",
                data: form_data,
                dataType: 'html',
                success: function(msg){
                    $('#banggia').empty();
                    $('#banggia').append(msg);
                }
            });
        }

    });
</script>